package br.com.jw.controle;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import br.com.jw.dao.PermissoesDao;
import br.com.jw.jdbc.Pool;
import br.com.jw.modelo.Permissoes;

public class ThreadPermissoes extends Thread {
	private Socket socket;
	boolean aux = false;
	private Pool pool = new Pool();

	public ThreadPermissoes(Socket socket) {
		this.socket = socket;
	}

	public void run(){
				
		try{
			DataInputStream in=new DataInputStream(socket.getInputStream());
			DataOutputStream out=new DataOutputStream(socket.getOutputStream());
			PermissoesDao permissoesDao=new PermissoesDao(pool); 
			   
			String modo=in.readUTF();
			 
			 if(modo.equals("listarPermissoesUsuario")){
				int usu_id=in.readInt();	   
				String dados="";
				for(Permissoes p:permissoesDao.listaPermissoesUsuario(usu_id) ){
					dados+=p.getPer_men_id()+",";
					dados+=p.getMen_nome()+",";										
				}
				
				out.writeUTF(dados);				
			
			out.close();
			in.close();
			socket.close();
		
		}
			 
			 else if(modo.equals("verificarPermissao")){			 
				 int per_usu_id=in.readInt();
				 int per_men_id=in.readInt();
				 
				 if(permissoesDao.verificarConcederPermissao(per_usu_id, per_men_id)){
					 out.writeBoolean(true);
				 }else{
					 out.writeBoolean(false);					 
				 }
				 out.close();
				 in.close();
				 socket.close();
			 }
			/* else if(modo.equals("concederPermissoes")){
				 
				 int per_men_id=in.readInt();
				 int usu_id=in.readInt();
				 if(permissoesDao.concederPermissoes(per_men_id,usu_id)){
					 out.writeBoolean(true);
				 }else{
					 out.writeBoolean(false);					 
				 }
				 out.close();
				 in.close();
				 socket.close();
			 }*/
			 
			 else if(modo.equals("excluirPermissoesUsuario")){
			 	int usu_id=in.readInt();
			 	int men_id=in.readInt();
			 	if(permissoesDao.excluirPermissoesUsuario(usu_id,men_id)){
			 		out.writeBoolean(true);
			 	}else{
			 		out.writeBoolean(false);
			 	}
			 	out.close();
				in.close();
				socket.close();
			 }
			 
		     else if(modo.equals("listarPermissoes")){
			 int usu_id=in.readInt();
				String dados="";
				for(Permissoes p :permissoesDao.getPermissoes(usu_id) ){
					dados+=p.getMen_nome()+",";					
				}
				
				out.writeUTF(dados);				
			
			out.close();
			in.close();
			socket.close();
		}
	
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
